Extracting and Visualizing Stock Data
Description
Extracting essential data from a dataset and displaying it is a necessary part of data science; therefore individuals can make correct decisions based on the data. In this assignment, you will extract some stock data, you will then display this data in a graph.
Table of Contents
- Define a Function that Makes a Graph
- Question 1: Use yfinance to Extract Stock Data
- Question 2: Use Webscraping to Extract Tesla Revenue Data
- Question 3: Use yfinance to Extract Stock Data
- Question 4: Use Webscraping to Extract GME Revenue Data
- Question 5: Plot Tesla Stock Graph
- Question 6: Plot GameStop Stock Graph
Estimated Time Needed: 30 min
*Note*:- If you are working Locally using anaconda, please uncomment the following code and execute it.
#!pip install yfinance==0.2.38
#!pip install pandas==2.2.2
#!pip install nbformat
!pip install yfinance==0.1.67
!mamba install bs4==4.10.0 -y
!pip install nbformat==4.2.0
!mamba install html5lib==1.1 -y
!pip install lxml==4.6.4
Requirement already satisfied: yfinance==0.1.67 in /home/jupyterlab/conda/envs/python/lib/python3.7/site-packages (0.1.67)
Requirement already satisfied: pandas>=0.24 in /home/jupyterlab/conda/envs/python/lib/python3.7/site-packages (from yfinance==0.1.67) (1.3.5)
Requirement already satisfied: numpy>=1.15 in /home/jupyterlab/conda/envs/python/lib/python3.7/site-packages (from yfinance==0.1.67) (1.21.6)
Requirement already satisfied: requests>=2.20 in /home/jupyterlab/conda/envs/python/lib/python3.7/site-packages (from yfinance==0.1.67) (2.29.0)
Requirement already satisfied: multitasking>=0.0.7 in /home/jupyterlab/conda/envs/python/lib/python3.7/site-packages (from yfinance==0.1.67) (0.0.11)
Requirement already satisfied: lxml>=4.5.1 in /home/jupyterlab/conda/envs/python/lib/python3.7/site-packages (from yfinance==0.1.67) (4.9.2)
Requirement already satisfied: python-dateutil>=2.7.3 in /home/jupyterlab/conda/envs/python/lib/python3.7/site-packages (from pandas>=0.24->yfinance==0.1.67) (2.8.2)
Requirement already satisfied: pytz>=2017.3 in /home/jupyterlab/conda/envs/python/lib/python3.7/site-packages (from pandas>=0.24->yfinance==0.1.67) (2023.3)
Requirement already satisfied: charset-normalizer<4,>=2 in /home/jupyterlab/conda/envs/python/lib/python3.7/site-packages (from requests>=2.20->yfinance==0.1.67) (3.1.0)
Requirement already satisfied: idna<4,>=2.5 in /home/jupyterlab/conda/envs/python/lib/python3.7/site-packages (from requests>=2.20->yfinance==0.1.67) (3.4)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in /home/jupyterlab/conda/envs/python/lib/python3.7/site-packages (from requests>=2.20->yfinance==0.1.67) (1.26.15)
Requirement already satisfied: certifi>=2017.4.17 in /home/jupyterlab/conda/envs/python/lib/python3.7/site-packages (from requests>=2.20->yfinance==0.1.67) (2023.5.7)
Requirement already satisfied: six>=1.5 in /home/jupyterlab/conda/envs/python/lib/python3.7/site-packages (from python-dateutil>=2.7.3->pandas>=0.24->yfinance==0.1.67) (1.16.0)
__ __ __ __
/ \ / \ / \ / \
/ \/ \/ \/ \
███████████████/ /██/ /██/ /██/ /████████████████████████
/ / \ / \ / \ / \ \____
/ / \_/ \_/ \_/ \ o \__,
/ _/ \_____/ `
|/
███╗ ███╗ █████╗ ███╗ ███╗██████╗ █████╗
████╗ ████║██╔══██╗████╗ ████║██╔══██╗██╔══██╗
██╔████╔██║███████║██╔████╔██║██████╔╝███████║
██║╚██╔╝██║██╔══██║██║╚██╔╝██║██╔══██╗██╔══██║
██║ ╚═╝ ██║██║ ██║██║ ╚═╝ ██║██████╔╝██║ ██║
╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚═╝╚═════╝ ╚═╝ ╚═╝
mamba (1.4.2) supported by @QuantStack
GitHub: https://github.com/mamba-org/mamba
Twitter: https://twitter.com/QuantStack
█████████████████████████████████████████████████████████████
Looking for: ['bs4==4.10.0']
[+] 0.0s
[+] 0.1s
pkgs/main/linux-64 ━━━━━━━━━╸━━━━━━━━━━━━━━━ 0.0 B / ??.?MB @ ??.?MB/s 0.1s
pkgs/main/noarch ╸━━━━━━━━━━━━━━━╸━━━━━━━━ 0.0 B / ??.?MB @ ??.?MB/s 0.1s
pkgs/r/linux-64 ━━━━━━━━━━━━╸━━━━━━━━━━━━ 0.0 B / ??.?MB @ ??.?MB/s 0.1s
pkgs/r/noarch ━━━╸━━━━━━━━━━━━━━━╸━━━━━ 0.0 B / ??.?MB @ ??.?MB/s 0.1spkgs/main/noarch No change
pkgs/r/linux-64 No change
pkgs/r/noarch No change
[+] 0.2s
pkgs/main/linux-64 ━━━━━━━━━━━╸━━━━━━━━━━━━━ 16.4kB / ??.?MB @ 107.1kB/s 0.2s[+] 0.3s
pkgs/main/linux-64 ━━━━━━━━━━━━━━╸━━━━━━━━━━ 471.1kB / ??.?MB @ 1.8MB/s 0.3s[+] 0.4s
pkgs/main/linux-64 ━━━━━━━━╸━━━━━━━━━━━━━━━━ 1.0MB / ??.?MB @ 2.8MB/s 0.4s[+] 0.5s
pkgs/main/linux-64 ━━━━━━━━━━━╸━━━━━━━━━━━━━ 1.6MB / ??.?MB @ 3.4MB/s 0.5s[+] 0.6s
pkgs/main/linux-64 ━━━━━━━━━━━━━╸━━━━━━━━━━━ 2.1MB / ??.?MB @ 3.7MB/s 0.6s[+] 0.7s
pkgs/main/linux-64 ━━━━━━━━━━━━━━━╸━━━━━━━━━ 2.7MB / ??.?MB @ 3.9MB/s 0.7s[+] 0.8s
pkgs/main/linux-64 ━━╸━━━━━━━━━━━━━━━╸━━━━━━ 3.2MB / ??.?MB @ 4.1MB/s 0.8s[+] 0.9s
pkgs/main/linux-64 ━━━━╸━━━━━━━━━━━━━━━╸━━━━ 3.6MB / ??.?MB @ 4.1MB/s 0.9s[+] 1.0s
pkgs/main/linux-64 ━━━━━━━╸━━━━━━━━━━━━━━━╸━ 4.1MB / ??.?MB @ 4.2MB/s 1.0s[+] 1.1s
pkgs/main/linux-64 ━━━━━━━━━╸━━━━━━━━━━━━━━━ 4.7MB / ??.?MB @ 4.3MB/s 1.1s[+] 1.2s
pkgs/main/linux-64 ━━━━━━━━━━━╸━━━━━━━━━━━━━ 5.2MB / ??.?MB @ 4.3MB/s 1.2s[+] 1.3s
pkgs/main/linux-64 ━━━━━━━━━━━━━╸━━━━━━━━━━━ 5.5MB / ??.?MB @ 4.4MB/s 1.3s[+] 1.4s
pkgs/main/linux-64 ━━━━━━━━━━━━━━╸━━━━━━━━━━ 5.8MB / ??.?MB @ 4.4MB/s 1.4s[+] 1.5s
pkgs/main/linux-64 ━━━━━━━━╸━━━━━━━━━━━━━━━━ 6.6MB / ??.?MB @ 4.5MB/s 1.5s[+] 1.6s
pkgs/main/linux-64 ━━━━━━━━━╸━━━━━━━━━━━━━━━ 6.8MB / ??.?MB @ 4.5MB/s 1.6s[+] 1.7s
pkgs/main/linux-64 ━━━━━━━━━━━━━━━━━━━━━━━━ 7.0MB @ 4.5MB/s Finalizing 1.7s[+] 1.8s
pkgs/main/linux-64 ━━━━━━━━━━━━━━━━━━━━━━━━ 7.0MB @ 4.5MB/s Finalizing 1.8spkgs/main/linux-64 @ 4.5MB/s 1.8s
Pinned packages:
- python 3.7.*
Transaction
Prefix: /home/jupyterlab/conda/envs/python
All requested packages already installed
Requirement already satisfied: nbformat==4.2.0 in /home/jupyterlab/conda/envs/python/lib/python3.7/site-packages (4.2.0)
Requirement already satisfied: ipython-genutils in /home/jupyterlab/conda/envs/python/lib/python3.7/site-packages (from nbformat==4.2.0) (0.2.0)
Requirement already satisfied: jsonschema!=2.5.0,>=2.4 in /home/jupyterlab/conda/envs/python/lib/python3.7/site-packages (from nbformat==4.2.0) (4.17.3)
Requirement already satisfied: jupyter-core in /home/jupyterlab/conda/envs/python/lib/python3.7/site-packages (from nbformat==4.2.0) (4.12.0)
Requirement already satisfied: traitlets>=4.1 in /home/jupyterlab/conda/envs/python/lib/python3.7/site-packages (from nbformat==4.2.0) (5.9.0)
Requirement already satisfied: attrs>=17.4.0 in /home/jupyterlab/conda/envs/python/lib/python3.7/site-packages (from jsonschema!=2.5.0,>=2.4->nbformat==4.2.0) (23.1.0)
Requirement already satisfied: importlib-metadata in /home/jupyterlab/conda/envs/python/lib/python3.7/site-packages (from jsonschema!=2.5.0,>=2.4->nbformat==4.2.0) (4.11.4)
Requirement already satisfied: importlib-resources>=1.4.0 in /home/jupyterlab/conda/envs/python/lib/python3.7/site-packages (from jsonschema!=2.5.0,>=2.4->nbformat==4.2.0) (5.12.0)
Requirement already satisfied: pkgutil-resolve-name>=1.3.10 in /home/jupyterlab/conda/envs/python/lib/python3.7/site-packages (from jsonschema!=2.5.0,>=2.4->nbformat==4.2.0) (1.3.10)
Requirement already satisfied: pyrsistent!=0.17.0,!=0.17.1,!=0.17.2,>=0.14.0 in /home/jupyterlab/conda/envs/python/lib/python3.7/site-packages (from jsonschema!=2.5.0,>=2.4->nbformat==4.2.0) (0.19.3)
Requirement already satisfied: typing-extensions in /home/jupyterlab/conda/envs/python/lib/python3.7/site-packages (from jsonschema!=2.5.0,>=2.4->nbformat==4.2.0) (4.5.0)
Requirement already satisfied: zipp>=3.1.0 in /home/jupyterlab/conda/envs/python/lib/python3.7/site-packages (from importlib-resources>=1.4.0->jsonschema!=2.5.0,>=2.4->nbformat==4.2.0) (3.15.0)
__ __ __ __
/ \ / \ / \ / \
/ \/ \/ \/ \
███████████████/ /██/ /██/ /██/ /████████████████████████
/ / \ / \ / \ / \ \____
/ / \_/ \_/ \_/ \ o \__,
/ _/ \_____/ `
|/
███╗ ███╗ █████╗ ███╗ ███╗██████╗ █████╗
████╗ ████║██╔══██╗████╗ ████║██╔══██╗██╔══██╗
██╔████╔██║███████║██╔████╔██║██████╔╝███████║
██║╚██╔╝██║██╔══██║██║╚██╔╝██║██╔══██╗██╔══██║
██║ ╚═╝ ██║██║ ██║██║ ╚═╝ ██║██████╔╝██║ ██║
╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚═╝╚═════╝ ╚═╝ ╚═╝
mamba (1.4.2) supported by @QuantStack
GitHub: https://github.com/mamba-org/mamba
Twitter: https://twitter.com/QuantStack
█████████████████████████████████████████████████████████████
Looking for: ['html5lib==1.1']
pkgs/main/linux-64 Using cache
pkgs/main/noarch Using cache
pkgs/r/linux-64 Using cache
pkgs/r/noarch Using cache
Pinned packages:
- python 3.7.*
Transaction
Prefix: /home/jupyterlab/conda/envs/python
Updating specs:
- html5lib==1.1
- ca-certificates
- certifi
- openssl
Package Version Build Channel Size
──────────────────────────────────────────────────────────────────────
Install:
──────────────────────────────────────────────────────────────────────
+ html5lib 1.1 pyhd3eb1b0_0 pkgs/main/noarch 93kB
+ webencodings 0.5.1 py37_1 pkgs/main/linux-64 20kB
Summary:
Install: 2 packages
Total download: 113kB
──────────────────────────────────────────────────────────────────────
[+] 0.0s
Downloading (1) ━━╸━━━━━━━━━━━━━━━━━━━━ 0.0 B webencodings 0.0s
Extracting ━━━━━━━━━━━━━━━━━━━━━━━ 0 0.0s[+] 0.1s
Downloading (2) ━━━━━━━━━━━━━━━━━━━━━━━ 0.0 B webencodings 0.1s
Extracting ━━━━━━━━━━━━━━━━━━━━━━━ 0 0.0swebencodings 19.6kB @ 131.0kB/s 0.1s
html5lib 93.0kB @ 577.0kB/s 0.2s
[+] 0.2s
Downloading ━━━━━━━━━━━━━━━━━━━━━━━ 112.6kB 0.2s
Extracting (2) ━━━━━━━╸━━━━━━━━━━━━━━━ 0 html5lib 0.0s[+] 0.3s
Downloading ━━━━━━━━━━━━━━━━━━━━━━━ 112.6kB 0.2s
Extracting (2) ━━━━━━━━╸━━━━━━━━━━━━━━ 0 html5lib 0.1s[+] 0.4s
Downloading ━━━━━━━━━━━━━━━━━━━━━━━ 112.6kB 0.2s
Extracting (2) ━━━━━━━━━╸━━━━━━━━━━━━━ 0 html5lib 0.2s[+] 0.5s
Downloading ━━━━━━━━━━━━━━━━━━━━━━━ 112.6kB 0.2s
Extracting (2) ━━━━━━━━━━╸━━━━━━━━━━━━ 0 html5lib 0.3s[+] 0.6s
Downloading ━━━━━━━━━━━━━━━━━━━━━━━ 112.6kB 0.2s
Extracting (2) ━━━━━━━━━━━╸━━━━━━━━━━━ 0 webencodings 0.4s[+] 0.7s
Downloading ━━━━━━━━━━━━━━━━━━━━━━━ 112.6kB 0.2s
Extracting (2) ━━━━━━━━━━━━━╸━━━━━━━━━ 0 webencodings 0.5s[+] 0.8s
Downloading ━━━━━━━━━━━━━━━━━━━━━━━ 112.6kB 0.2s
Extracting (2) ━━━━━━━━╸━━━━━━━━━━━━━━ 0 webencodings 0.6s[+] 0.9s
Downloading ━━━━━━━━━━━━━━━━━━━━━━━ 112.6kB 0.2s
Extracting (2) ━━━━━━━━━╸━━━━━━━━━━━━━ 0 webencodings 0.7s
Downloading and Extracting Packages
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
Collecting lxml==4.6.4
Downloading lxml-4.6.4-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_24_x86_64.whl (6.3 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 6.3/6.3 MB 84.7 MB/s eta 0:00:00:00:0100:01
Installing collected packages: lxml
Attempting uninstall: lxml
Found existing installation: lxml 4.9.2
Uninstalling lxml-4.9.2:
Successfully uninstalled lxml-4.9.2
Successfully installed lxml-4.6.4
import yfinance as yf
import pandas as pd
import requests
from bs4 import BeautifulSoup
import plotly.graph_objects as go
from plotly.subplots import make_subplots
In Python, you can ignore warnings using the warnings module. You can use the filterwarnings function to filter or ignore specific warning messages or categories.
import warnings
# Ignore all warnings
warnings.filterwarnings("ignore", category=FutureWarning)
Define Graphing Function¶
In this section, we define the function make_graph. You don't have to know how the function works, you should only care about the inputs. It takes a dataframe with stock data (dataframe must contain Date and Close columns), a dataframe with revenue data (dataframe must contain Date and Revenue columns), and the name of the stock.
def make_graph(stock_data, revenue_data, stock):
fig = make_subplots(rows=2, cols=1, shared_xaxes=True, subplot_titles=("Historical Share Price", "Historical Revenue"), vertical_spacing = .3)
stock_data_specific = stock_data[stock_data.Date <= '2021--06-14']
revenue_data_specific = revenue_data[revenue_data.Date <= '2021-04-30']
fig.add_trace(go.Scatter(x=pd.to_datetime(stock_data_specific.Date, infer_datetime_format=True), y=stock_data_specific.Close.astype("float"), name="Share Price"), row=1, col=1)
fig.add_trace(go.Scatter(x=pd.to_datetime(revenue_data_specific.Date, infer_datetime_format=True), y=revenue_data_specific.Revenue.astype("float"), name="Revenue"), row=2, col=1)
fig.update_xaxes(title_text="Date", row=1, col=1)
fig.update_xaxes(title_text="Date", row=2, col=1)
fig.update_yaxes(title_text="Price ($US)", row=1, col=1)
fig.update_yaxes(title_text="Revenue ($US Millions)", row=2, col=1)
fig.update_layout(showlegend=False,
height=900,
title=stock,
xaxis_rangeslider_visible=True)
fig.show()
Question 1: Use yfinance to Extract Stock Data¶
Using the Ticker function enter the ticker symbol of the stock we want to extract data on to create a ticker object. The stock is Tesla and its ticker symbol is TSLA.
tesla=yf.Ticker("TSLA")
Using the ticker object and the function history extract stock information and save it in a dataframe named tesla_data. Set the period parameter to max so we get information for the maximum amount of time.
tesla_history=tesla.history(period="max")
tesla_data=pd.DataFrame(tesla_history)
tesla_data
| Open | High | Low | Close | Volume | Dividends | Stock Splits | |
|---|---|---|---|---|---|---|---|
| Date | |||||||
| 2010-06-29 | 1.266667 | 1.666667 | 1.169333 | 1.592667 | 281494500 | 0 | 0.0 |
| 2010-06-30 | 1.719333 | 2.028000 | 1.553333 | 1.588667 | 257806500 | 0 | 0.0 |
| 2010-07-01 | 1.666667 | 1.728000 | 1.351333 | 1.464000 | 123282000 | 0 | 0.0 |
| 2010-07-02 | 1.533333 | 1.540000 | 1.247333 | 1.280000 | 77097000 | 0 | 0.0 |
| 2010-07-06 | 1.333333 | 1.333333 | 1.055333 | 1.074000 | 103003500 | 0 | 0.0 |
| ... | ... | ... | ... | ... | ... | ... | ... |
| 2024-05-31 | 178.500000 | 180.320007 | 173.820007 | 178.080002 | 67314600 | 0 | 0.0 |
| 2024-06-03 | 178.130005 | 182.639999 | 174.490005 | 176.289993 | 68568900 | 0 | 0.0 |
| 2024-06-04 | 174.779999 | 177.759995 | 174.000000 | 174.770004 | 60056300 | 0 | 0.0 |
| 2024-06-05 | 175.350006 | 176.149994 | 172.130005 | 175.000000 | 57953800 | 0 | 0.0 |
| 2024-06-06 | 174.600006 | 179.729996 | 172.729996 | 177.940002 | 69578400 | 0 | 0.0 |
3509 rows × 7 columns
Reset the index using the reset_index(inplace=True) function on the tesla_data DataFrame and display the first five rows of the tesla_data dataframe using the head function. Take a screenshot of the results and code from the beginning of Question 1 to the results below.
tesla_data.reset_index(inplace=True)
tesla_data
| Date | Open | High | Low | Close | Volume | Dividends | Stock Splits | |
|---|---|---|---|---|---|---|---|---|
| 0 | 2010-06-29 | 1.266667 | 1.666667 | 1.169333 | 1.592667 | 281494500 | 0 | 0.0 |
| 1 | 2010-06-30 | 1.719333 | 2.028000 | 1.553333 | 1.588667 | 257806500 | 0 | 0.0 |
| 2 | 2010-07-01 | 1.666667 | 1.728000 | 1.351333 | 1.464000 | 123282000 | 0 | 0.0 |
| 3 | 2010-07-02 | 1.533333 | 1.540000 | 1.247333 | 1.280000 | 77097000 | 0 | 0.0 |
| 4 | 2010-07-06 | 1.333333 | 1.333333 | 1.055333 | 1.074000 | 103003500 | 0 | 0.0 |
| ... | ... | ... | ... | ... | ... | ... | ... | ... |
| 3504 | 2024-05-31 | 178.500000 | 180.320007 | 173.820007 | 178.080002 | 67314600 | 0 | 0.0 |
| 3505 | 2024-06-03 | 178.130005 | 182.639999 | 174.490005 | 176.289993 | 68568900 | 0 | 0.0 |
| 3506 | 2024-06-04 | 174.779999 | 177.759995 | 174.000000 | 174.770004 | 60056300 | 0 | 0.0 |
| 3507 | 2024-06-05 | 175.350006 | 176.149994 | 172.130005 | 175.000000 | 57953800 | 0 | 0.0 |
| 3508 | 2024-06-06 | 174.600006 | 179.729996 | 172.729996 | 177.940002 | 69578400 | 0 | 0.0 |
3509 rows × 8 columns
Question 2: Use Webscraping to Extract Tesla Revenue Data¶
Use the requests library to download the webpage https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0220EN-SkillsNetwork/labs/project/revenue.htm Save the text of the response as a variable named html_data.
url = "https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0220EN-SkillsNetwork/labs/project/revenue.htm"
response = requests.get(url)
html_data = response.text
Parse the html data using beautiful_soup.
soup = BeautifulSoup(html_data, 'html.parser')
tables = soup.find_all('table')
tesla_revenue = pd.read_html(str(tables))[0]
tesla_revenue.columns = ['Date', 'Revenue']
print(tesla_revenue.head())
Date Revenue 0 2021 $53,823 1 2020 $31,536 2 2019 $24,578 3 2018 $21,461 4 2017 $11,759
Using BeautifulSoup or the read_html function extract the table with Tesla Revenue and store it into a dataframe named tesla_revenue. The dataframe should have columns Date and Revenue.
Click here if you need help locating the table
Below is the code to isolate the table, you will now need to loop through the rows and columns like in the previous lab
soup.find_all("tbody")[1]
If you want to use the read_html function the table is located at index 1
We are focusing on quarterly revenue in the lab.
read_html_pandas_data = pd.read_html(url)
Execute the following line to remove the comma and dollar sign from the Revenue column.
tesla_revenue["Revenue"] = tesla_revenue['Revenue'].str.replace(',|\$',"")
Execute the following lines to remove an null or empty strings in the Revenue column.
tesla_revenue.dropna(inplace=True)
tesla_revenue = tesla_revenue[tesla_revenue['Revenue'] != ""]
Display the last 5 row of the tesla_revenue dataframe using the tail function. Take a screenshot of the results.
tesla_revenue.tail()
| Date | Revenue | |
|---|---|---|
| 8 | 2013 | 2013 |
| 9 | 2012 | 413 |
| 10 | 2011 | 204 |
| 11 | 2010 | 117 |
| 12 | 2009 | 112 |
Question 3: Use yfinance to Extract Stock Data¶
Using the Ticker function enter the ticker symbol of the stock we want to extract data on to create a ticker object. The stock is GameStop and its ticker symbol is GME.
game_stop=yf.Ticker("GME")
Using the ticker object and the function history extract stock information and save it in a dataframe named gme_data. Set the period parameter to max so we get information for the maximum amount of time.
history_game_stop=game_stop.history(period="max")
gme_data=pd.DataFrame(history_game_stop)
Reset the index using the reset_index(inplace=True) function on the gme_data DataFrame and display the first five rows of the gme_data dataframe using the head function. Take a screenshot of the results and code from the beginning of Question 3 to the results below.
gme_data.reset_index(inplace=True)
gme_data.head()
| Date | Open | High | Low | Close | Volume | Dividends | Stock Splits | |
|---|---|---|---|---|---|---|---|---|
| 0 | 2002-02-13 | 1.620129 | 1.693350 | 1.603296 | 1.691667 | 76216000 | 0.0 | 0.0 |
| 1 | 2002-02-14 | 1.712707 | 1.716074 | 1.670626 | 1.683250 | 11021600 | 0.0 | 0.0 |
| 2 | 2002-02-15 | 1.683250 | 1.687458 | 1.658001 | 1.674834 | 8389600 | 0.0 | 0.0 |
| 3 | 2002-02-19 | 1.666417 | 1.666417 | 1.578047 | 1.607503 | 7410400 | 0.0 | 0.0 |
| 4 | 2002-02-20 | 1.615920 | 1.662210 | 1.603296 | 1.662210 | 6892800 | 0.0 | 0.0 |
Question 4: Use Webscraping to Extract GME Revenue Data¶
Use the requests library to download the webpage https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0220EN-SkillsNetwork/labs/project/stock.html. Save the text of the response as a variable named html_data.
URL="https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0220EN-SkillsNetwork/labs/project/stock.html"
html_data=requests.get(URL).text
Parse the html data using beautiful_soup.
soup=BeautifulSoup(html_data,'html.parser')
Using BeautifulSoup or the read_html function extract the table with GameStop Revenue and store it into a dataframe named gme_revenue. The dataframe should have columns Date and Revenue. Make sure the comma and dollar sign is removed from the Revenue column using a method similar to what you did in Question 2.
Click here if you need help locating the table
Below is the code to isolate the table, you will now need to loop through the rows and columns like in the previous lab
soup.find_all("tbody")[1]
If you want to use the read_html function the table is located at index 1
tables=soup.find_all("table")
gme_revenue=pd.read_html(str(tables))[0]
gme_revenue.columns=["Date","Revenue"]
gme_revenue["Revenue"] = gme_revenue['Revenue'].str.replace(',|\$',"")
gme_revenue.dropna(inplace=True)
gme_revenue = gme_revenue[gme_revenue['Revenue'] != ""]
Display the last five rows of the gme_revenue dataframe using the tail function. Take a screenshot of the results.
gme_revenue.tail()
| Date | Revenue | |
|---|---|---|
| 11 | 2009 | 8806 |
| 12 | 2008 | 7094 |
| 13 | 2007 | 5319 |
| 14 | 2006 | 3092 |
| 15 | 2005 | 1843 |
Question 5: Plot Tesla Stock Graph¶
Use the make_graph function to graph the Tesla Stock Data, also provide a title for the graph. The structure to call the make_graph function is make_graph(tesla_data, tesla_revenue, 'Tesla'). Note the graph will only show data upto June 2021.
tesla_revenue['Date'] = pd.to_datetime(tesla_revenue['Date'], format='%Y')
make_graph(tesla_data, tesla_revenue, 'Tesla')
Question 6: Plot GameStop Stock Graph¶
Use the make_graph function to graph the GameStop Stock Data, also provide a title for the graph. The structure to call the make_graph function is make_graph(gme_data, gme_revenue, 'GameStop'). Note the graph will only show data upto June 2021.
gme_revenue['Date'] = pd.to_datetime(gme_revenue['Date'], format='%Y')
make_graph(gme_data, gme_revenue, 'GameStop')
About the Authors:
Joseph Santarcangelo has a PhD in Electrical Engineering, his research focused on using machine learning, signal processing, and computer vision to determine how videos impact human cognition. Joseph has been working for IBM since he completed his PhD.
Azim Hirjani
Change Log¶
| Date (YYYY-MM-DD) | Version | Changed By | Change Description |
|---|---|---|---|
| 2022-02-28 | 1.2 | Lakshmi Holla | Changed the URL of GameStop |
| 2020-11-10 | 1.1 | Malika Singla | Deleted the Optional part |
| 2020-08-27 | 1.0 | Malika Singla | Added lab to GitLab |
© IBM Corporation 2020. All rights reserved.